package cn.wolfcode.cmis.servlet;

import cn.wolfcode.cmis.dao.IEmployeeDAO;
import cn.wolfcode.cmis.dao.lmpl.EmployeeDAOImpl;
import cn.wolfcode.cmis.domain.Employee;
import cn.wolfcode.cmis.domain.User;
import cn.wolfcode.cmis.service.IUserService;
import cn.wolfcode.cmis.service.Impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    IUserService userService=new UserServiceImpl();
    IEmployeeDAO employeeDAO =new EmployeeDAOImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User user1  = null;
        try {
            user1 = userService.checkUser(user);
            //获取session保存数据
            HttpSession httpSession = req.getSession();
            httpSession.setAttribute("USER_IN_SESSION",user1);
            resp.sendRedirect("/lol/employee");
        } catch (Exception e) {
            e.printStackTrace();
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }

    }
}

